package com.zhuoyian.analysis.service;

import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.zhuoyian.analysis.util.JSONHelper;
import com.zhuoyian.analysis.util.StaticSettings;

public class BaseService {
	/**
	 * 
	 * @param object
	 * @return
	 */
	public ARModel getARModel(JSONObject object) {
		ARModel model = null;
		try {
			if (object != null) {
				model = JSONHelper.parseObject(object, ARModel.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}

	public String GetAPIUrl(String Method) {
		return StaticSettings.getServerPath() + "Api/" + Method;
	}

	public synchronized void getJsonObjectRequest(final String url,
			final Handler handler, final List<NameValuePair> param) {
		new Thread() {
			public void run() {
				try {
					String json = WebService.GetResponse(url, param);
					if (json != null) {
						JSONObject object = new JSONObject(json);
						ARModel model = getARModel(object);
						Bundle bundle = new Bundle();
						bundle.putSerializable("result", model);
						Message message = new Message();
						message.what = StaticSettings.RESULT_OK;
						message.setData(bundle);
						handler.sendMessage(message);
					} else {
						handler.sendEmptyMessage(StaticSettings.RESULT_ERROR);
					}
				} catch (Exception e) {
					handler.sendEmptyMessage(StaticSettings.RESULT_ERROR);
					e.printStackTrace();
				}
			}
		}.start();
	}
}
